﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using bus=WpfDataBingding.TaskServiceReference;

namespace WpfDataBingding
{
    /// <summary>
    /// DevDetail.xaml 的交互逻辑
    /// </summary>
    public partial class DevDetail : UserControl
    {        

        public DevDetail()
        {
            InitializeComponent();
            //初始化隐藏时隙输入模块
            this.MonTimeInputDockPanel.Visibility = System.Windows.Visibility.Hidden;
        }

        /// <summary>
        /// 将DevDetail的数据源属性绑定到DevInfo_lay
        /// </summary>
        new public Object DataContext 
        {
            get { return this.DevInfo_lay.DataContext; }
            set { this.DevInfo_lay.DataContext = value; }
        }

        /// <summary>
        /// 添加&删除对某个设备的监控
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_DelAndAddDevMon_Click(object sender, RoutedEventArgs e)
        {
            bool isMonitered;
            WpfDataBingding.DevTreeData.Device dev_selected;
            try
            {   
                //获取设备当前的监控状态
                dev_selected = ((WpfDataBingding.DevTreeData.Device)this.DataContext);
                isMonitered=dev_selected.IsMonitered;
            }
            catch (Exception exp)
            {
                MessageBox.Show("内部错误："+exp.Message);
                return;
            }

            if (!isMonitered)//未监控--添加监控:显示输入时隙模版
            {
                this.MonTimeInputDockPanel.Visibility = System.Windows.Visibility.Visible;
                return;
            }
            else//已被监控--删除监控
            {
                //建立客户服务代理对象
                bus.DataBusServiceClient dBSC = new bus.DataBusServiceClient();
                try
                {
                    //删除监控
                    MonTaskConfigration.deleteMonDevTask(dBSC, dev_selected);
                    this.MonStatus_Label.Content = dev_selected.MonitorStatusLabel;//修改界面
                    this.Button_DelAndAddDevMon.Content = dev_selected.MonitorStatusButton;    
                }
                catch (Exception exp)
                {
                    MessageBox.Show("连接远程服务出错：" + exp.Message);
                    return;
                }
                finally
                {
                    if (dBSC != null)
                        dBSC.Close();
                }
            }

        }



        /// <summary>
        /// 从界面获取时间间隙参数
        /// </summary>
        /// <returns></returns>
        private int getTimeInterval()
        {            
            try
            {
               int num= Convert.ToInt32(this.timeBox.Text);
               return num;
            }
            catch (Exception )
            {                
                throw new ApplicationException("时间间隙输入不合法");
            }
        }

        /// <summary>
        /// "确定"添加监控
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addMonTask_Button_Click(object sender, RoutedEventArgs e)
        {
            WpfDataBingding.DevTreeData.Device dev_selected = ((WpfDataBingding.DevTreeData.Device)this.DataContext);
            //建立客户服务代理对象            
            bus.DataBusServiceClient dBSC = new bus.DataBusServiceClient();
            try
            {
                //获取监控时间间隔
                int timeInterval= getTimeInterval();
                //添加设备监控
                MonTaskConfigration.addMonDevTask(dBSC, dev_selected, timeInterval);
                //隐藏时隙输入模版
                this.MonTimeInputDockPanel.Visibility = System.Windows.Visibility.Hidden;                
                this.MonStatus_Label.Content = dev_selected.MonitorStatusLabel;//修改界面
                this.Button_DelAndAddDevMon.Content = dev_selected.MonitorStatusButton; 
            }
            catch (Exception exp)
            {
                MessageBox.Show("出错：" + exp.Message);
                return;
            }
            finally
            {
                if (dBSC != null)
                    dBSC.Close();
            }           
        }

    }
}
